/*
 * @Author: hmhvictory 1400628624@qq.com
 * @Date: 2025-07-24 16:08:20
 * @LastEditors: hmhvictory 1400628624@qq.com
 * @LastEditTime: 2025-07-24 16:08:45
 * @FilePath: \实训笔记\day09\demo1.java
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package day09;

import java.util.Random;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        // 生成一个长度为 20 的正整型数组
        int[] array = new int[20];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100) + 1;
        }
        System.out.print("生成的数组为：");
        for (int num : array) {
            System.out.print(num + " ");
        }
        System.out.println();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            // 等待用户输入要查找的数字
            System.out.print("请输入要查找的数字（输入 -1 结束程序）：");
            int num = scanner.nextInt();
            if (num == -1) {
                System.out.println("程序已停止运行。");
                break;
            }
            // 查找数字在数组中的位置
            boolean found = false;
            for (int i = 0; i < array.length; i++) {
                if (array[i] == num) {
                    System.out.println("数字 " + num + " 在数组中的下标位置为：" + i);
                    found = true;
                }
            }
            if (!found) {
                System.out.println("数据不存在");
            }
        }
    }
}
